#pragma once
#include <stack>
#include "types.h"
#include "NonCopyable.h"
#include "Point.h"
#include "Size.h"
#include <iostream>
#include <future>
class SkColor
{
public:
	SkColor(unsigned short r, unsigned short g, unsigned short b, unsigned short alpha = 255);
	SkColor();

	~SkColor();
	
	short R();
	short G();
	short B();
	short Alpha();

private:
	short m_r;
	short m_g;
	short m_b;
	short m_alpha;
};

class SkFont
{
public:
	SkFont(std::wstring& name, unsigned int fontSize = 12);
	~SkFont();

	void setFontSize(unsigned int size);
	void setBold(bool bold);
	void setItalic(bool italic);
	void setUnderline(bool underline);
	void setStrikeout(bool strikeout);

	std::wstring& fontName();
	unsigned int fontSize();
	bool bold();
	bool italic();
	bool underline();
	bool strikeout();

	gfx::Size getSize(const Gdiplus::StringFormat& strFormat, const std::wstring& szText);

private:
	std::wstring m_fontName;
	unsigned int m_fontSize;
	bool m_isBold;
	bool m_isUnderline;
	bool m_isStrikeout;
	bool m_isItalic;
};

class SkCanvas : public NonCopyable                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
{
public:
	SkCanvas(unsigned int w, unsigned int h);
	SkCanvas(HDC dc, unsigned int w, unsigned int h);
	~SkCanvas();

public:
	bool setSize(unsigned int w, unsigned int h);

	bool onPaint(HDC hdc);

	bool setFont(SkFont& font);
	// bool drawImage();
	bool drawRect(SkColor& color, int x, int y, int width, int height);
	bool FillRect(SkColor& color, int x, int y, int width, int height);
	bool drawText(std::wstring& str, SkFont& font, SkColor& color, int x, int y, int w, int h);
	bool drawText(wchar_t str, SkFont& font, SkColor& color, int x, int y, int w, int h);
	bool TextOut(std::wstring& str, int x, int y);
	bool FillPaths(SkColor& color, gfx::Point* points, int points_num, int x, int y);

	bool drawLine(SkColor& color, int x1, int y1, int x2, int y2);

	bool drawPicture(Gdiplus::Bitmap& bitmap, int x, int y, int w, int h);

	gfx::Size measureString(std::wstring& str, SkFont& font);
	int getFontHeight();
	int getFontWidth();

	void save();
	void restore();
	bool clipRect(int x, int y, int w, int h);
	bool translate(int x, int y);
	bool rotate(float angle);


	Gdiplus::Graphics* getCurrentGraphics() const;
private:
	bool changeGraphicsSize();


private:
	struct Layer
	{
		std::shared_ptr<Gdiplus::Graphics> mem_graphics;
		std::shared_ptr<Gdiplus::Bitmap> mem_bitmap;

		uint8 alpha;
	};

	unsigned int m_width;
	unsigned int m_height;
	std::stack<Layer*> layers_;
	std::shared_ptr<Gdiplus::Bitmap> mem_base_bitmap_;
	std::shared_ptr<Gdiplus::Graphics> mem_base_graphics_;
	std::stack<Gdiplus::GraphicsState> states_;

	HDC device_dc_;
	HDC mem_dc_;
	HBITMAP h_bitmap_;

	HFONT h_new_font_;
	HGDIOBJ h_old_font_;
};

